package com.think.oauth.user.dao.user;

import java.util.Set;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.think.oauth.user.model.Permission;
import com.think.oauth.user.model.Role;
import com.think.oauth.user.model.User;

@Mapper
public interface UserDao extends BaseMapper<User> {

	@Select("SELECT think_user.* FROM think_user LEFT JOIN (SELECT * FROM think_oauth) oauth ON think_user.id=oauth.user_id  WHERE identifier='${identifier}' AND login_type=${loginType} ")
	public User selectUser(@Param("identifier")String identifier,@Param("loginType")Integer loginType);
	
	@Select("SELECT * FROM think_user WHERE think_user.id=#{userId} ")
	public User selectUserById(Integer userId);
	
	@Insert("INSERT INTO think_user (nickname,name,avatar_url,phone,city,country,province,subscribe,gender) "
			+ "VALUES (#{nickname},#{name},#{avatarUrl},#{phone},#{city},#{country},#{province},#{subscribe},#{gender})")
	@Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")
	Integer insertUserWithPhone(User user);
	
	@Select("SELECT * FROM think_oauth WHERE WHERE identifier='${identifier}' AND login_type=${loginType}  ")
	Integer selectUserIdWithOauth(@Param("identifier")String identifier,@Param("loginType")Integer loginType);
	
	@Update("UPDATE think_user SET nickname=#{nickname},name=#{name},avatar_url=#{avatarUrl},phone=#{phone},city=#{city},country=#{country},province=#{province},gender=#{gender} "
			+ " WHERE id=#{id}")
	public Integer updateUser(User user);
	
	@Select("select * from think_permission where permission_id in (select permission_id from think_role_permission where role_id in (select role_id from think_user_role where user_id = #{userId}))")
    Set<Permission> getUserPermissions(Integer userId);
	
}
